/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.staf.NetworkReportGenerator;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.LineNumberReader;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author aezhov
 */
public class FileSnmpWalker  {
    public static final String MAC_OID_D = ".1.3.6.1.2.1.17.4.3.1.2"; 
    public static final String MAC_OID_Q = ".1.3.6.1.2.1.17.7.1.2.2.1.2";
    public static final String IF_OPER_STATUS = ".1.3.6.1.2.1.2.2.1.8";

    HashMap<String, HashSet<String>> portToMac = new HashMap<String, HashSet<String>>();
    HashMap<String, HashSet<String>> portToIp = new HashMap<String, HashSet<String>>();
    HashMap<String, Boolean> portsUpDown = new HashMap<String, Boolean>();
    HashMap<String, String> snmpPortmap = null;

    String fileSep = System.getProperty("file.separator");
    NmapXMLParser nmapParser = null;

    public FileSnmpWalker(Switch sw, HashMap<String, String> snmpPortmap,
            NmapXMLParser nmapParser) 
    {
        this.snmpPortmap = snmpPortmap;
        this.nmapParser = nmapParser;

        String spoolDirName = nmapParser.getSpoolDir();
        try {
            parse(sw.snmpDump);
        } catch (java.io.FileNotFoundException exc) {
            System.out.println("FileSnmpWalker. FileNotFound " + exc);
        } catch (java.io.IOException exc) {
            System.out.println("FileSnmpWalker. IOException " + exc);
        }
    }

    public HashMap<String, HashSet<String>> portToMacMap() {
        return portToMac;
    }

    public HashMap<String, HashSet<String>> portToIpMap() {
        return portToIp;
    }

    public HashMap<String, Boolean> portsUpDownMap() {
        return portsUpDown;
    }

    void parse(String filepath) throws java.io.FileNotFoundException,
                                       java.io.IOException
    {
        FileReader fileReader = new FileReader(filepath);
        LineNumberReader reader = new LineNumberReader(fileReader);
        String line = null;

        while ((line = reader.readLine()) != null) {
            String mac = null;
            String port = null;
            int updownPort = 0;
            boolean up = false;

            if (line.startsWith(MAC_OID_Q)) {
                String[] parts = line.split("[. ]");
                mac = toMacString(parts[15], parts[16], parts[17], parts[18], parts[19], parts[20]);
                port = parts[23];
            } else if (line.startsWith(MAC_OID_D)) {
                String[] parts = line.split("[. ]");
                mac = toMacString(parts[12], parts[13], parts[14], parts[15], parts[16], parts[17]);
                port = parts[20];
            } else if (line.startsWith(IF_OPER_STATUS)) {
                String[] parts = line.split("[. ]");
                if (parts[14].equals("up(1)")) {
                    updownPort = new Integer(parts[11]).intValue();
                    up = true;
                } else if (parts[14].equals("down(2)")) {
                    updownPort = new Integer(parts[11]).intValue();
                    up = false;
                }
            }

            if (updownPort != 0) {
                String ifName = snmpPortmap.get(new Integer(updownPort).toString());
                System.out.println("Port " + ifName + " is up ? " + up);
                portsUpDown.put(ifName, new Boolean(up));
            }

            if (mac != null) {
                String ifName = snmpPortmap.get(port);
                System.out.println("FileSnmpWalker. MAC: " + mac + " Port: " + ifName);
                HashSet<String> macs = portToMac.get(ifName);
                if (macs == null) {
                    macs = new HashSet<String>();
                    portToMac.put(ifName, macs);
                }
                macs.add(mac);

                Set ipSet = nmapParser.getIpsForMac(mac);
                HashSet<String> ips = portToIp.get(ifName);
                if (ips == null) {
                    ips = new HashSet<String>();
                    portToIp.put(ifName, ips);
                }
                if (ipSet != null)
                    ips.addAll(ipSet);
             }
        }
    }

    String toMacString(String o1, String o2, String o3, String o4, String o5, String o6) {
        System.out.println(o1 + " " + o2 + " " + o3 + " " + o4 + " " + o5 + " " + o6);
        
        return String.format("%02x:%02x:%02x:%02x:%02x:%02x", new Integer(o1).intValue(),
                                                       new Integer(o2).intValue(),
                                                       new Integer(o3).intValue(),
                                                       new Integer(o4).intValue(),
                                                       new Integer(o5).intValue(),
                                                       new Integer(o6).intValue()).toUpperCase();

    }
}
